<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';
	/**
	 * @brief This class Describes the Saved_Queries Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 24 MAR 2011
	 * 	 - <b>Author</b>: Jack Farmer
	 * 	  - Initial Version
	 *
	 * @author Jack Farmer
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_Saved_Queries extends cTable
	{
		const C_INT_MAX_RESULTS_RETURNED = 500;
		const C_INT_DEFAULT_RESULTS_RETURNED = 100;
		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_UserID = null;
		public $m_int_COL_Query = null;
		public $m_int_COL_QueryTitle = NULL;
		public $m_int_COL_SourceID = NULL;
		public $m_int_COL_LastRun = null;
		public $m_int_COL_NextRun = null;
		public $m_int_COL_Update = null;
		public $m_int_COL_Hour = null;
		public $m_int_COL_Day = null;
		public $m_int_COL_DayWeek = null;
		public $m_int_COL_Month = null;
		public $m_int_COL_Max = null;
		public $m_int_COL_Schedule = null;
		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "Saved_Queries";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColVarChar("id");
			$obj_COL_ID->set_Length(36);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of users.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_UserID = new cColVarChar("user_id");
			$obj_COL_UserID->set_Length(36);
			$obj_COL_UserID->set_Comment("This column holds the User GUID.");
			$obj_COL_UserID->set_Label("User GUID");

			$obj_COL_SourceID = new cColText("source_id");
			$obj_COL_SourceID->set_Comment("This column holds the Source info object.");
			$obj_COL_SourceID->set_Label("Source GUID");
			
			$obj_COL_SourceName = new cColVarChar("source_name");
			$obj_COL_SourceName->set_Length(36);
			$obj_COL_SourceName->set_Comment("This column holds the Source name.");
			$obj_COL_SourceName->set_Label("Source NAME");

			$obj_COL_QueryTitle = new cColVarChar("q_title");
			$obj_COL_QueryTitle->set_Length(255);
			$obj_COL_QueryTitle->set_Comment("This column holds the name of the Query the user wishes to save.");
			$obj_COL_QueryTitle->set_Label("Query Title");

			$obj_COL_QueryString = new cColText("q_string");
			$obj_COL_QueryString->set_Comment("This column holds the Query the user wishes to save.");
			$obj_COL_QueryString->set_Label("Query String");

			$obj_COL_LastRun = new cColDateTime("last_run");
			$obj_COL_LastRun->set_Comment("This column holds the last run time.");
			$obj_COL_LastRun->set_Label("Last Run");

			$obj_COL_NextRun = new cColDateTime("next_run");
			$obj_COL_NextRun->set_Comment("This column holds the next run time.");
			$obj_COL_NextRun->set_Label("Next Run");

			$obj_COL_Update = new cColDateTime("update");
			$obj_COL_Update->set_Comment("This column holds the last updated time of saved query.");
			$obj_COL_Update->set_Label("Update");

			$obj_COL_THour = new cColVarChar("t_hour");
			$obj_COL_THour->set_Length(100);
			$obj_COL_THour->set_Comment("This column holds the update hour.");
			$obj_COL_THour->set_Label("Hour");

			$obj_COL_TDay = new cColVarChar("t_day");
			$obj_COL_TDay->set_Length(13);
			$obj_COL_TDay->set_Comment("This column holds the update day.");
			$obj_COL_TDay->set_Label("Day");

			$obj_COL_TDayWeek = new cColVarChar("t_dayofweek");
			$obj_COL_TDayWeek->set_Length(13);
			$obj_COL_TDayWeek->set_Comment("This column holds the update day of the week.");
			$obj_COL_TDayWeek->set_Label("Day of the week");

			$obj_COL_TMonth = new cColVarChar("t_month");
			$obj_COL_TMonth->set_Length(4);
			$obj_COL_TMonth->set_Comment("This column holds the update month.");
			$obj_COL_TMonth->set_Label("Month");

			$obj_COL_Max = new cColSmallInt("max");
			$obj_COL_Max->set_Length(6);
			$obj_COL_Max->set_Comment("This column holds the max return results.");
			$obj_COL_Max->set_Label("Max Return");

			$obj_COL_Schedule = new cColTinyInt("scheduled");
			$obj_COL_Schedule->set_Length(1);
			$obj_COL_Schedule->set_Comment("This column holds the scheduled bool check.");
			$obj_COL_Schedule->set_Label("Scheduled");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_HIDDEN);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//			$this->m_obj_Connection->activate_Logging();
			//			$this->m_obj_Connection->activate_LogDebug();
			//			$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_UserID = $this->add_Column($obj_COL_UserID);
			$this->m_int_COL_SourceID = $this->add_Column($obj_COL_SourceID);
			$this->m_int_COL_SourceName = $this->add_Column($obj_COL_SourceName);
			$this->m_int_COL_QueryTitle = $this->add_Column($obj_COL_QueryTitle);
			$this->m_int_COL_Query = $this->add_Column($obj_COL_QueryString);
			$this->m_int_COL_LastRun = $this->add_Column($obj_COL_LastRun);
			$this->m_int_COL_NextRun = $this->add_Column($obj_COL_NextRun);
			$this->m_int_COL_Update = $this->add_Column($obj_COL_Update);
			$this->m_int_COL_Hour = $this->add_Column($obj_COL_THour);
			$this->m_int_COL_Day = $this->add_Column($obj_COL_TDay);
			$this->m_int_COL_DayWeek = $this->add_Column($obj_COL_TDayWeek);
			$this->m_int_COL_Month = $this->add_Column($obj_COL_TMonth);
			$this->m_int_COL_Max = $this->add_Column($obj_COL_Max);
			$this->m_int_COL_Schedule = $this->add_Column($obj_COL_Schedule);
		}
		static public function encode($str_Data)
		{
			$str_Data = base64_encode(serialize($str_Data));
			return $str_Data;
		}

		static public function decode($str_Data)
		{
			$str_Data = unserialize(base64_decode($str_Data));
			return $str_Data;
		}
		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			if (!$this->get_max())
			{
				$this->set_max(self::C_INT_DEFAULT_RESULTS_RETURNED);
			}
			else
			{
				if (($this->get_max() < 1) || ($this->get_max() > self::C_INT_MAX_RESULTS_RETURNED))
				{
					$this->set_max(self::C_INT_MAX_RESULTS_RETURNED);
				}
			}

			$this->set_ColumnValue($this->m_int_COL_Update, date("Y/m/d H:i:s"));

			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}

			return false;
		}
		/**
		 * This function is used to update data that is already in the database
		 */
		public function update_Data2DB()
		{
			$this->clear_Update();
			$this->add_ColumnQuery($this->m_int_COL_ID, $this->get_id());

			return parent::update_Data2DB();
		}
		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			$this->set_id($this->create_GUID());

			return parent::insert_Data2DB();
		}

		/**
		 *
		 * This function is used to delete Data from the database
		 * @param string $str_UserID
		 */
		public function delete_Query($str_ID)
		{
			$this->clear_Query();
			$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
			if ($this->do_Delete() < 0)
			{
				return false;
			}
			return true;
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param string $str_User_ID ID of the User to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($str_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}

		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the Query to check (GUID)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if (!empty($str_ID) && preg_match(self::c_REG_GUID_VALID, $str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}

		/**
		 *
		 * Enter description here ...
		 * @param unknown_type $source_ID
		 */

		public function getSourceName($source_ID)
		{
			$t_source_name = '';
			switch ($source_ID)
			{
				case ampleharvest\code\cConfig::C_STR_SOURCE_REAPER:
					$t_source_name = 'Reaper';
					break;
				case ampleharvest\code\cConfig::C_STR_SOURCE_OCTOBERSKY:
					$t_source_name = 'OctoberSky';
					break;
				case ampleharvest\code\cConfig::C_STR_SOURCE_GROUNDEFFECT:
					$t_source_name = 'GroundEffect';
					break;
			}
			return $t_source_name;
		}
	}
?>
